from manim import *



class math1(MovingCameraScene):
    def construct(self):
        self.camera.background_color = BLACK  # 设置背景颜色
        self.camera.frame_width = 100  # 设置边框宽度
        self.camera.frame_height = 60  # 设置边框高度
        self.camera.pixel_height = 1080  # 设置像素高度
        self.camera.pixel_width = 1920  # 设置像素宽度
        self.camera.center = ORIGIN  # 设置中心点位置
        self.camera.scale_factor = 1.0  # 设置缩放因子
        #设置横线
        for i in range(6*2+1):       
            dot1=Dot([-50,5*(i-6),0]).set_opacity(0.5)
            dot2=Dot([50,5*(i-6),0]).set_opacity(0.5)
            if i==6:
                line1=Line(dot1,dot2).set_color(WHITE).set_opacity(0.5)
                
            else:
                line1=Line(dot1,dot2).set_color(WHITE).set_opacity(0.5)
               
            self.add(dot1,dot2,line1)
        #设置竖线
        for i in range(10*2+1):        
            dot3=Dot([(i-10)*5,-30,0]).set_opacity(0.5)
            dot4=Dot([(i-10)*5,30,0]).set_opacity(0.5)
            if i==10:
                line2=Line(dot3,dot4).set_color(WHITE).set_opacity(0.5)
                
            else:
                line2=Line(dot3,dot4).set_color(WHITE).set_opacity(0.5)
            self.add(dot3,dot4,line2)
        #设置三个点
        dot1 = Dot(radius=1, color=RED)  
        dot1.move_to([-47.5,27.5,0]) 
        dot2 = Dot(radius=1, color=YELLOW)  
        dot2.move_to([-42.5,27.5,0])  
        dot3 = Dot(radius=1, color=GREEN)  
        dot3.move_to([-37.5,27.5,0])
        #镜头跟进效果 
        self.camera.frame.save_state()
        #题目出现
          #文本
        text = Tex(r"\text{秘籍2:链式法则}",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,0,0])
        self.play(Write(text),self.camera.frame.animate.scale(0.8))
        self.wait()
        self.play(Restore(self.camera.frame),text.animate.scale(1).move_to([-40,25,0]))

        text1 = Tex(r"\text{对于}$z=f\left( u,v\right) ,$\text{其中}$\begin{cases}u=u\left( x,y\right) \\v=v\left( x,y\right) \end{cases}\text{则有}$",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(10.0).move_to([0,15,0])
        text2 = Tex(r"$\dfrac{\partial z}{\partial x}=$",color=BLUE
                   ,tex_template=TexTemplateLibrary.ctex).scale(10.0).move_to([-35,0,0])

        self.play(Write(text1))
        self.play(Write(text2))
        rectangle=Rectangle(color=BLUE,fill_opacity=0.0,width=3,height=5
                            ,stroke_color=RED,stroke_width=30)
        rectangle.move_to([-20, 15, 0])
        self.play(Create(rectangle))

        text3 = Tex(r"$f_{1}$",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(10.0).next_to(text2,RIGHT,buff=2)
        self.play(Write(text3))

        rectangle1=Rectangle(color=BLUE,fill_opacity=0.0,width=26,height=6
                            ,stroke_color=YELLOW,stroke_width=30)
        rectangle1.move_to([17.5, 19, 0])
        self.play(Create(rectangle1))
        text4 = Tex(r"$\dfrac{\partial u}{\partial x}$",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(10.0).next_to(text3,RIGHT,buff=2)
        self.play(Write(text4))

        text5 = Tex(r"$+$",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(10.0).next_to(text4,RIGHT,buff=2)
        self.play(Write(text5))
        text6 = Tex(r"$f_{2}$",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(10.0).next_to(text5,RIGHT,buff=2)
        text7 = Tex(r"$\dfrac{\partial v}{\partial x}$",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(10.0).next_to(text6,RIGHT,buff=2)
        self.play(rectangle.animate().shift(5*RIGHT),Write(text6))
        self.wait(0.5)
        self.play(rectangle1.animate().shift(8.5*DOWN),Write(text7))
        self.wait(0.5)
        self.play(FadeOut(rectangle),FadeOut(rectangle1))


        text8 = Tex(r"$\dfrac{\partial z}{\partial y}=$",color=BLUE
                   ,tex_template=TexTemplateLibrary.ctex).scale(10.0).move_to([-35,-14,0])

        self.play(Write(text8))
        rectangle2=Rectangle(color=BLUE,fill_opacity=0.0,width=3,height=5
                            ,stroke_color=RED,stroke_width=30)
        rectangle2.move_to([-20, 15, 0])
        self.play(Create(rectangle2))

        text9 = Tex(r"$f_{1}$",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(10.0).next_to(text8,RIGHT,buff=2)
        self.play(Write(text9))

        rectangle3=Rectangle(color=BLUE,fill_opacity=0.0,width=26,height=6
                            ,stroke_color=YELLOW,stroke_width=30)
        rectangle3.move_to([17.5, 19, 0])
        self.play(Create(rectangle3))
        text10 = Tex(r"$\dfrac{\partial u}{\partial y}$",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(10.0).next_to(text9,RIGHT,buff=2)
        self.play(Write(text10))

        text11 = Tex(r"$+$",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(10.0).next_to(text10,RIGHT,buff=2)
        self.play(Write(text11))
        text12 = Tex(r"$f_{2}$",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(10.0).next_to(text11,RIGHT,buff=2)
        text13 = Tex(r"$\dfrac{\partial v}{\partial y}$",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(10.0).next_to(text12,RIGHT,buff=2)
        self.play(rectangle2.animate().shift(5*RIGHT),Write(text12))
        self.wait(0.5)
        self.play(rectangle3.animate().shift(8.5*DOWN),Write(text13))
        self.wait(0.5)
        self.play(Uncreate(text4),Uncreate(text5),Uncreate(text6),Uncreate(text7)
                  ,Uncreate(text8),Uncreate(text9),Uncreate(text10)
                  ,Uncreate(text11),Uncreate(text12),Uncreate(text13)
                  ,Uncreate(text3),Uncreate(text2),Uncreate(text1)
                  ,FadeOut(rectangle3),FadeOut(rectangle2))




         #题目出现
        text = Tex(r"\text{(张1000)设函数f和g均可微,}$z=f\left[ xy,\ln x+g\left( xy\right) \right] $"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-12.5,0,0])
        text100 = Tex(r"\text{则}$x\dfrac{\partial z}{ax}-y\dfrac{\partial z}{\partial y}=$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text,RIGHT,buff=1)
        self.add(text, text100)
         # 使用 updater 让子对象跟随父对象
        def update_child(mob):
            mob.next_to(text,RIGHT,buff=1)
        
        text100.add_updater(update_child)

        text1 = Tex(
                   r"\text{(A)}$f_{1}$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-32,-10,0])
        text2 = Tex(
                    r"\text{(B)}$f_{2}$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-12,-10,0])
        text3 = Tex(
                   r"\text{(C)}$f_{1}+f_{2}$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([12,-10,0])
        text4 = Tex(
                    r"\text{(D)}$f_{1}-f_{2}$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([32,-10,0])

        self.play(Write(text),Write(text1),Write(text2),Write(text3),Write(text4),Write(text100))
        self.wait(0.1)
        #题目向上移动
        self.play(text.animate().move_to([-12.5,20,0]),text1.animate().move_to([-32,10,0]) ,text2.animate().move_to([-12,10,0]),text3.animate().move_to([12,10,0]),text4.animate().move_to([32,10,0]) )

        

        text2 = Tex(r"$\dfrac{\partial z}{\partial x}=$",color=BLUE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-35,0,0])

       
        self.play(Write(text2))
        

        text3 = Tex(r"$f_{1}$",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text2,RIGHT,buff=2)
        self.play(Write(text3))

        
        text4 = Tex(r"$y$",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text3,RIGHT,buff=2)
        self.play(Write(text4))

        text5 = Tex(r"$+$",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text4,RIGHT,buff=2)
        self.play(Write(text5))
        text6 = Tex(r"$f_{2}$",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text5,RIGHT,buff=2)
        text7 = Tex(r"$[\dfrac{1}{x}+yg'\left( xy\right) ]$",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text6,RIGHT,buff=2)
        self.play(Write(text6))
        self.play(Write(text7))
        text8 = Tex(r"$x\dfrac{\partial z}{\partial x}=xyf_{1}+\left[ 1+xyg^{'}\left( xy\right) \right] f_{2}$",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([25,0,0])
        self.play(Write(text8))
        text2 = Tex(r"$\dfrac{\partial z}{\partial y}=$",color=BLUE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-35,-10,0])
        
       
        self.play(Write(text2))
        

        text3 = Tex(r"$f_{1}$",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text2,RIGHT,buff=2)
        self.play(Write(text3))

        
        text4 = Tex(r"$x$",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text3,RIGHT,buff=2)
        self.play(Write(text4))

        text5 = Tex(r"$+$",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text4,RIGHT,buff=2)
        self.play(Write(text5))
        text6 = Tex(r"$f_{2}$",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text5,RIGHT,buff=2)
        text7 = Tex(r"$xg^{'}\left( xy\right) $",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text6,RIGHT,buff=2)
        self.play(Write(text6))
        self.play(Write(text7))
        text8 = Tex(r"$y\dfrac{\partial z}{\partial y}=xyf_{1}+xyg^{'}\left( xy\right) f_{2}$",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([25,-10,0])
        self.play(Write(text8))

         #半透明矩阵扩展
        rectangle4=Rectangle(color=BLUE,fill_opacity=0.5,width=1,height=2.8
                            ,stroke_color=YELLOW,stroke_width=0).move_to([-12,10,0])
        self.play(
            rectangle4.animate.stretch(15.0,dim=0)
        )

        self.wait()















        self.wait()